﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CodePlex.Silverlight.Controls
{
    [TemplatePart(Name = "Normal State",   Type = typeof(Storyboard))]
    [TemplatePart(Name = "Disabled State", Type = typeof(Storyboard))]
    [TemplatePart(Name = "RootElement", Type = typeof(FrameworkElement))] 
    [TemplatePart(Name = "NavigationControlElement", Type = typeof(StackPanel))] 
    [TemplatePart(Name = "ContentHostElement", Type = typeof(Grid))] 
    [TemplatePart(Name = "ContentBorderElement", Type = typeof(FrameworkElement))] 
    public class NavigationPane : ItemsControl
    {
        internal const string ElementContentBorderName = "ContentBorderElement";
        internal const string ElementContentHostName = "ContentHostElement";
        internal const string ElementRootName = "RootElement";
        internal const string ElementNavigationControlName = "NavigationControlElement";
        internal const string StoryBoardNormalState = "Normal State";
        internal const string StoryBoardDisabledState = "Disabled State";

        public FrameworkElement ElementRoot { get; set; }
        public StackPanel ElementNavigationPanel { get; set; }
        public FrameworkElement ElementContentBorder { get; set; }
        public Grid ElementContentHost { get; set; }
        internal Storyboard StateDisabled { get; set; }
        internal Storyboard StateNormal { get; set; }


        public static readonly DependencyProperty SelectedHeaderProperty =
           DependencyProperty.Register("SelectedHeader", typeof(string),
                                       typeof(NavigationPane), null);

        public string SelectedHeader
        {
            get { return (string)GetValue(SelectedHeaderProperty); }
            set { SetValue(SelectedHeaderProperty, value); }
        }



        public static readonly DependencyProperty SelectedItemProperty =
           DependencyProperty.Register("SelectedItem", typeof(object),
                                       typeof(NavigationPane), new PropertyChangedCallback(NavigationPane.OnSelectedItemChanged));      

        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }       
             

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            
            this.ElementRoot = base.GetTemplateChild(ElementRootName) as FrameworkElement;
            this.ElementNavigationPanel = base.GetTemplateChild(ElementNavigationControlName) as StackPanel;
            this.ElementContentBorder = base.GetTemplateChild(ElementContentBorderName) as FrameworkElement;
            this.ElementContentHost = base.GetTemplateChild(ElementContentHostName) as Grid;
            if (this.ElementRoot != null)
            {
                this.StateNormal = this.ElementRoot.Resources[StoryBoardNormalState] as Storyboard;
                this.StateDisabled = this.ElementRoot.Resources[StoryBoardDisabledState] as Storyboard;
            }
            
            if (this.ElementNavigationPanel != null)
            {
                NavigationItem firstItem=null;
                
                foreach (NavigationItem item in base.Items)
                {
                    if (firstItem == null)
                    {
                        firstItem = item;
                        
                    }
                    this.ElementNavigationPanel.Children.Add(item);
                    this.ElementContentHost.Children.Add(item.Content as UIElement);    
                }
                firstItem.IsSelected = true;
                SelectedItem = firstItem;
                firstItem.DefaultItem=true;
                
            }

        }

       
        private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NavigationPane control = (NavigationPane) d;
            if (e.OldValue != e.NewValue)
            {
                NavigationItem newValue = e.NewValue as NavigationItem;
                control.ElementContentHost.Children.Clear();
                UIElement content = newValue.Content as UIElement;
                control.ElementContentHost.Children.Add(content);
                foreach (NavigationItem item in control.Items)
                {      
                    item.IsSelected=(item==newValue);
                }
                control.SelectedHeader = newValue.Header.ToString();
            }
        }
     

    }
}
